<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2012.03.17.
   家居商家发布数据统计控制器($小牛New $2014.07.28)*/

!defined('Q_PATH') && exit;

class JiajucompanycountController extends AController{

	public function init__(){
		$this->_arrNotMethod=array(
			'save_previewstatus',
			'save_preview',
			'view_preview',
			'foreverdelete',
			'foreverdelete_deep',
			'input_change_ajax',
			'input_change_unique',
			'forbid',
			'closeitem',
			'openitem',
			'resume',
			'clear_recycle',
			'save_sort',
			'insert',
			'add',
			'edit',
			'update'
		);
		
		parent::init__();
	}

	public function index($sName=null,$bDisplay=true){
		$arrData=array();

		$arrCurLimit=$GLOBALS['_commonConfig_']['SHOP_LIMIT'][$GLOBALS['_company_']['jiajucompany_isfvip']];
		$nStartDay=date('Y-m-01',time());
		$nEndDay=date('Y-m-01',strtotime("{$nStartDay} +1 month"));
		$nStartDay=strtotime($nStartDay);
		$nEndDay=strtotime($nEndDay);
		
		// 获取统计
		$oCompany=Q::instance('JiajucompanyModel');
		$arrData['count']['case']=$oCompany->getNum('case');// 已发布
		$arrData['count']['room']=$oCompany->getNum('case',array('jiajucase_ismodel'=>1));
		$arrData['count']['event']=$oCompany->getNum('event',array('string_'=>'create_dateline between '.$nStartDay.' AND '.$nEndDay));
		$arrData['count']['diary']=$oCompany->getNum('diary');
		$arrData['count']['anli']=$oCompany->getNum('anli');
		$arrData['count']['package']=$oCompany->getNum('package');
		$arrData['count']['video']=$oCompany->getNum('video');
		$arrData['count']['build']=$oCompany->getNum('build');
		$arrData['count']['designer']=$oCompany->getNum('designer',array(),array(0,1,2,11,3,12,13));
		$arrData['count']['order']=$oCompany->getNum('order',array(),array(0,1,9,13));
		$arrData['count']['comment']=$oCompany->getNum('companyguestbook',array(),array(0,1,2,11,3,12,13));
		$arrData['count']['guestbook']=$oCompany->getNum('companyguestbook',array(),array(0,1,2,11,3,12,13));
		$arrData['ok']['case']=$oCompany->getNum('case',array(),array(1));// 审核通过
		$arrData['ok']['room']=$oCompany->getNum('case',array('jiajucase_ismodel'=>1),array(1));
		$arrData['ok']['event']=$oCompany->getNum('event',array(),array(1));
		$arrData['ok']['diary']=$oCompany->getNum('diary',array(),array(1));
		$arrData['ok']['anli']=$oCompany->getNum('anli',array(),array(1));
		$arrData['ok']['package']=$oCompany->getNum('package',array(),array(1));
		$arrData['ok']['video']=$oCompany->getNum('video',array(),array(1));
		$arrData['ok']['build']=$oCompany->getNum('build',array(),array(1));
		$arrData['ok']['designer']=$oCompany->getNum('designer',array(),array(1));
		$arrData['ok']['order']=$oCompany->getNum('order',array(),array(1));
		$arrData['ok']['comment']=$oCompany->getNum('companyguestbook',array(),array(1));
		$arrData['ok']['guestbook']=$oCompany->getNum('companyguestbook',array(),array(1));

		foreach($arrData['count'] as $sKey=>$nValue){
			if(isset($arrCurLimit[strtoupper($sKey)])){
				$arrData['allow'][$sKey]=$arrCurLimit[strtoupper($sKey)]-$arrData['count'][$sKey];
				($arrData['allow'][$sKey]<0) && ($arrData['allow'][$sKey]=0);
			}
		}

		// 点评评分
		$oDb=Db::RUN();
		$arrData['comment']=$oDb->getRow("SELECT SUM(jiajucompanycomment_level)/COUNT(1) AS ca,SUM(jiajucompanycomment_level1)/COUNT(1) AS c1,SUM(jiajucompanycomment_level2)/COUNT(1) AS c2,SUM(jiajucompanycomment_level3)/COUNT(1) AS c3,SUM(jiajucompanycomment_level4)/COUNT(1) AS c4,SUM(jiajucompanycomment_level5)/COUNT(1) AS c5,count(1) AS num FROM  `".Q::C('DB_PREFIX')."jiajucompanycomment` WHERE jiajucompany_id = {$GLOBALS['_company_']['jiajucompany_id']} AND jiajucompanycomment_status=1 GROUP BY jiajucompany_id");

		if(!$arrData['comment']){
			$arrData['comment']=array('ca'=>3,'c1'=>'3.0','c2'=>'3.0','c3'=>'3.0','c4'=>'3.0','c5'=>'3.0','num'=>0);
		}
		foreach($arrData['comment'] as $sK=>$v){
			if($sK=='ca'){
				$arrData['comment'][$sK]=round($v);
			}elseif($sK!='num'){
				$arrData['comment'][$sK]=round($v,1);
			}
		}

		// 刷新商铺统计缓存
		if(Q::G('cache','G')==1){
			Model::M_('jiajucompany')->updateWhere(
				array(
					'jiajucompany_casenum'=>$arrData['ok']['case'],
					'jiajucompany_roomnum'=>$arrData['ok']['room'],
					'jiajucompany_eventnum'=>$arrData['ok']['event'],
					'jiajucompany_diarynum'=>$arrData['ok']['diary'],
					'jiajucompany_anlinum'=>$arrData['ok']['anli'],
					'jiajucompany_packagenum'=>$arrData['ok']['package'],
					'jiajucompany_videonum'=>$arrData['ok']['video'],
					'jiajucompany_buildnum'=>$arrData['ok']['build'],
					'jiajucompany_designednum'=>$arrData['ok']['designer'],
					'jiajucompany_ordernum'=>$arrData['ok']['order'],
					'jiajucompany_commentnum'=>$arrData['ok']['comment'],
					'jiajucompany_guestbooknum'=>$arrData['ok']['guestbook'],
					'jiajucompanycomment_level'=>$arrData['comment']['ca'],
					'jiajucompanycomment_level1'=>$arrData['comment']['c1'],
					'jiajucompanycomment_level2'=>$arrData['comment']['c2'],
					'jiajucompanycomment_level3'=>$arrData['comment']['c3'],
					'jiajucompanycomment_level4'=>$arrData['comment']['c4'],
					'jiajucompanycomment_level5'=>$arrData['comment']['c5'],
				),
				'jiajucompany_id=?',$GLOBALS['_company_']['jiajucompany_id']
			);

			$this->S('更新商铺审核通过统计缓存成功');
		}

		$this->assign('arrData',$arrData);
		$this->assign('arrVipLimit',$GLOBALS['_commonConfig_']['SHOP_LIMIT'][1]);
		$this->display();
	}

}
